#include<iostream>
#include<unistd.h>
#include<signal.h>
#include<cstdlib>
#include<functional>
#include<vector>
#include<sys/types.h>
#include<wait.h>
using namespace std;

typedef function<void()> fun;

vector<fun> callbacks;
int count = 0;
void showCount()
{
    cout<<"final count: "<<count<<endl;
}

void showLog()
{
    cout<<"日志功能"<<endl;
}

void logUser()
{
    if(fork()==0)
    {
        execl("/usr/bin/who","who",nullptr);
        exit(1);
    }
    wait(nullptr);
}

void catchSig(int signal)
{
    for(auto &f:callbacks)
    {
        f();
    }
    alarm(1);
}

static void Usage(string proc)
{
    cout << "Usage:\r\n\t" << proc << " signumber processid" << endl;
}


void handler(int signal)
{
    sleep(1);
    cout<<"捕获信号："<<signal<<endl;


    // cout<<count<<endl;
    // alarm(1);
    // exit(1);

}
int main()
{
    signal(SIGSEGV,handler);
    int *p = nullptr;
    *p = 100;


    //-----------------------------------------
    // signal(SIGALRM,catchSig);
    // alarm(1);
    // callbacks.push_back(showCount);
    // callbacks.push_back(showLog);
    // callbacks.push_back(logUser);
    // while(true)
    // {
    //     // cout<<count++<<endl;
    //     ++count;
    // }
    return 0;
}